package com.cole.gx095.service.impl;

import com.cole.gx095.Repository.StockgoodsRepository;
import com.cole.gx095.domain.BillCorData;
import com.cole.gx095.service.StockService;
import com.cole.gx095.entity.Stockgoods;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.List;

@Service
public class StockServiceImpl implements StockService {
    @Autowired
    StockgoodsRepository stockgoodsRepository;
    @Override
    public Page<Stockgoods> findAllBy(Stockgoods stockgoods, Integer currentPage, Integer pageSize) {
        currentPage--;
        currentPage = currentPage < 0 ? 0 : currentPage;//page 为页码，数据库从0页开始
        //可以使用重载的 of(int page, int size, Sort sort) 方法指定排序字段
        Pageable pageable = PageRequest.of(currentPage, pageSize);

        Specification<Stockgoods> purspecification = new Specification<Stockgoods>() {
            @Override
            public Predicate toPredicate(Root<Stockgoods> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();

                if (stockgoods.getContractNum()!=null&&stockgoods.getContractNum()!="")
                    predicateList.add(criteriaBuilder.equal(root.get("contractNum").as(String.class),stockgoods.getContractNum()));
                if (stockgoods.getBillNum()!=null&&stockgoods.getBillNum()!="")
                    predicateList.add(criteriaBuilder.equal(root.get("billNum").as(String.class),stockgoods.getBillNum()));
                if (stockgoods.getTypeName()!=null&&stockgoods.getTypeName()!="")
                    predicateList.add(criteriaBuilder.equal(root.get("typeName").as(String.class),stockgoods.getTypeName()));
                if (stockgoods.getGoodsName()!=null&&stockgoods.getGoodsName()!="")
                    predicateList.add(criteriaBuilder.equal(root.get("goodsName").as(String.class),stockgoods.getGoodsName()));
                if (stockgoods.getModle()!=null&&stockgoods.getModle()!="")
                    predicateList.add(criteriaBuilder.equal(root.get("modle").as(String.class),stockgoods.getModle()));
                if (stockgoods.getRemakes()!=null&&stockgoods.getRemakes()!="")
                    predicateList.add(criteriaBuilder.equal(root.get("remakes").as(String.class),stockgoods.getRemakes()));

                Predicate[] predicates = new Predicate[predicateList.size()];
                return query.where(predicateList.toArray(predicates)).getRestriction();
            }
        };
        return stockgoodsRepository.findAll(purspecification,pageable);//无数据时返回空列表;
    }

    @Override
    public List<Stockgoods> findAllBy(BillCorData billCorData) {
        Specification<Stockgoods> purspecification = new Specification<Stockgoods>() {
            @Override
            public Predicate toPredicate(Root<Stockgoods> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                List<Predicate> predicateList = new ArrayList<>();
                if (billCorData.getTypeName()!=null&&billCorData.getTypeName()!="")
                    predicateList.add(criteriaBuilder.equal(root.get("typeName").as(String.class),billCorData.getTypeName()));
                if (billCorData.getGoodsName()!=null&&billCorData.getGoodsName()!="")
                    predicateList.add(criteriaBuilder.equal(root.get("goodsName").as(String.class),billCorData.getGoodsName()));
                if (billCorData.getModle()!=null&&""!=billCorData.getModle())
                    predicateList.add(criteriaBuilder.equal(root.get("modle").as(String.class),billCorData.getModle()));

                Predicate[] predicates = new Predicate[predicateList.size()];
                return query.where(predicateList.toArray(predicates)).getRestriction();
            }
        };
        return stockgoodsRepository.findAll(purspecification);//无数据时返回空列表;
    }

}
